package com.bis.ymyy.controller;

import com.bis.ymyy.common.utils.Result;
import com.bis.ymyy.entity.Employee;
import com.bis.ymyy.service.EmployeeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employeement")
@Api(tags = "/test接口")
@Slf4j
public class EmployeeController {

    @Autowired(required = false)
    private EmployeeService employeeService;

    @GetMapping("/findAllEmployee")
    public Result<List> findAllAppointment() {
        Result<List> result = new Result<>();
        List<Employee> employeeList = employeeService.getAllEmployee();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setSuccess(true);
        result.setData(employeeList);
        return result;
    }

    @PostMapping("/blurredQuery")
    public Result<List> blurredQuery(@RequestBody Map<String,String> map) {
        Result<List> result = new Result<>();
        List<Employee> employeeList = employeeService.getAllEmployee();
        List<Employee> list = new ArrayList<>();
        if (map.get("name").equals("") && map.get("state").equals("")) {
            result.setCode(200);
            result.setMsg("查询成功,条件无");
            result.setSuccess(true);
            result.setData(employeeList);
        } else if ((!map.get("name").equals("")) && map.get("state").equals("")) {
//            employeeList.forEach((item) -> {
//                if (item.getName().equals(map.get("name"))) {
//                    result.setData(item);
//                }
//            });
            for (Employee e : employeeList) {
                if (map.get("name").equals(e.getName())) {
                    list.add(e);
                    result.setCode(200);
                    result.setMsg("查询成功");
                    result.setSuccess(true);
                }
            }
            result.setData(list);
        } else if (map.get("name").equals("") && !map.get("state").equals("")) {
            for (Employee e : employeeList) {
                if (map.get("state").equals(e.getState())) {
                    list.add(e);
                }
            }
            result.setCode(200);
            result.setMsg("查询成功");
            result.setSuccess(true);
            result.setData(list);
        } else {
            for (Employee e : employeeList) {
                if (map.get("state").equals(e.getState()) && map.get("name").equals(e.getName())) {
                    list.add(e);
                }
            }
            result.setCode(200);
            result.setMsg("查询成功");
            result.setSuccess(true);
            result.setData(list);
        }
        return result;
    }

    @PostMapping("/addEmployee")
    public Result<Employee> addEmployee(@RequestBody Employee employee) {
        Result<Employee> result = new Result<>();
        if (employee == null) {
            result.setCode(400);
            result.setMsg("参数为空");
            result.setSuccess(false);
        }
        if (employeeService.findById(employee.getId()) != null) {
            result.setCode(400);
            result.setMsg("id重复，添加失败");
            result.setSuccess(false);
        } else {
            employeeService.addOneEmployee(employee);
            result.setCode(200);
            result.setMsg("添加成功");
            result.setSuccess(true);
        }
        return result;
    }

    @GetMapping("/deleteById")
    public Result deleteEmployee(@RequestParam int id) {
        Result result = new Result();
        result.setCode(400);
        result.setMsg("删除失败");
        result.setSuccess(false);
        if (employeeService.findById(id) != null) {
            employeeService.deleteEmployee(id);
            result.setCode(200);
            result.setMsg("删除成功");
            result.setSuccess(true);
        } else {
            result.setCode(400);
            result.setMsg("该id下的员工不存在");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("/modifyEmployee")
    public Result<Employee> modifyEmployee(@RequestBody Employee employee) {
        Result<Employee> result = new Result<>();
        if (employeeService.modifyEmployee(employee) == 1) {
            result.setSuccess(true);
            result.setData(employeeService.findById(employee.getId()));
            result.setCode(200);
            result.setMsg("修改成功");
        } else {
            result.setCode(400);
            result.setMsg("修改失败");
            result.setSuccess(false);
        }
        return result;
    }
}
